package main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.InfModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.reasoner.Reasoner;
import com.hp.hpl.jena.reasoner.ReasonerRegistry;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;

public class Semantics {

	String nameSpace;
	final String filename = "main/task1.owl";
	final String snmpGetCmd = "c:\\usr\\bin\\snmpget -v 2c -c ttm4128v2 snmp-server.item.ntnu.no";
	
	public Semantics(){
		
		this.nameSpace = "http://www.item.ntnu.no/fag/ttm4128/sematicweb-set1#";
		
	}
		
	public String getMIBObjectName(String in){
		
		OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM,null);
		Reasoner reasoner = ReasonerRegistry.getOWLReasoner();
		Model model = FileManager.get().loadModel(filename);
		
		InputStream is = FileManager.get().open(filename);
		
		InfModel infModel = ModelFactory.createInfModel(reasoner, model);
		
		ontModel.read(is,"");
		
		if(is == null){
			throw new IllegalArgumentException("File: "+ filename +" not found");
		}
		
		
		Resource alias = ontModel.getResource(nameSpace+in);
		OntClass mibs = ontModel.getOntClass(nameSpace+"mibObjects");
		
		ExtendedIterator<OntClass> subClasses = mibs.listSubClasses();
		
		Resource comparator;
		
		String result;
		
		while(subClasses.hasNext()){
			OntClass subClass = subClasses.next();
			String localName = subClass.getLocalName();
			comparator = ontModel.getResource(nameSpace+localName);
			
			result = getMIBName(infModel, alias, null, null, comparator);
		
			if(result != null)
				return result;
		}

		return in+ " is not an Alias";
	}
	
	private String getMIBName(Model model, Resource r1, Property property, Resource r2, Resource r3){
		
		StmtIterator iterator = model.listStatements(r1, property, r2);
		
		while(iterator.hasNext()){
			
			Statement s = iterator.nextStatement();
			
			System.out.println(s.getResource().getLocalName());
			
			if(s.getObject().equals(r3)){
				return s.getResource().getLocalName();
			}
			
		}
		return null;
	}
	
	
	public String getValue(String in){
		
		String mib = getMIBObjectName(in);
		
		if(mib.equals(in+ " is not an Alias")){
			return "Error. "+in +" is a wrong MIB identifier / Alias.";
		}
		else{
						
			Runtime rt = Runtime.getRuntime();
			String queryResult = null;
			
			try {
				
				String cmd = snmpGetCmd +" "+ mib+".0";
				Process pr = rt.exec(cmd);
				
				BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
				
				String line = null;
				
				while((line=input.readLine()) != null){

					if(line.contains("=")){
						
						queryResult = line.substring(line.indexOf("=")+1).trim();
						
					}
				}
			} 
			catch (IOException e) {
				e.printStackTrace();
			}
			
			if(queryResult != null)
				return queryResult;
			else
				return "ERROR";
		}
	}
	
	public static void main(String[] args){
		Semantics sem = new Semantics();
		
		System.out.println(sem.getMIBObjectName("tcp"));
	}
	
}
